package com.zx.erp.market.controller;


import com.github.pagehelper.PageInfo;
import com.zx.erp.annonation.MyLog;
import com.zx.erp.market.bean.Custom;
import com.zx.erp.market.service.CustomService;
import com.zx.erp.sys.bean.City;
import com.zx.erp.sys.bean.Province;
import com.zx.erp.sys.bean.Users;
import com.zx.erp.sys.service.CityService;
import com.zx.erp.sys.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/market/customerBrowse")
public class CustomerBrowseController {
    @Autowired
    private CustomService cbiz;
    @Autowired
    private CityService citybiz;
    @Autowired
    private ProvinceService pbiz;

    //每次执行前会优先 将mid放入request范围
    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 7);
    }


    @RequestMapping("/findPageAll")
    @MyLog("客户浏览方法")
    public String findPageAll(Custom custom, Integer pid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model, HttpSession session){
        List<Integer> cidList=new ArrayList<Integer>();
        List<City> cityList =new ArrayList<City>();
        if(custom.getAddress()==null || custom.getAddress()==0){
            if(pid!=null){
                cityList=citybiz.findByPid(pid);
                for (City c:cityList){
                    cidList.add(c.getId());
                }
            }
        }else{
            cidList.add(custom.getAddress());
            model.addAttribute("cid",custom.getAddress());
        }
        Users users = (Users) session.getAttribute("users");
        if (users.getJobId()!=1){
            custom.setServiceid(users.getUId());
        }
        PageInfo<Custom> pb = cbiz.findPageAll(custom, cidList, null, pageNum, 6);
        model.addAttribute("pb",pb);

        model.addAttribute("custom", custom);

        List<Province> provinceList = pbiz.findAll();
        model.addAttribute("provinceList", provinceList);
        model.addAttribute("cityList", cityList);
        model.addAttribute("pid", pid);

        return "forward:/market/customerBrowse/customerBrowse.html";
    }
}
